package com.test.down

import android.Manifest
import android.annotation.SuppressLint
import android.content.Intent
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.util.Log
import android.widget.Button
import android.widget.TextView
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.download.manager.M3u8DownLoadService
import com.download.manager.utils.M3u8FileUtil
import com.test.down.adapter.MainLiveAdapter
import com.test.down.utils.PermissionUtil
import com.test.down.utils.PlayResource

class MainActivity : AppCompatActivity() {
    @SuppressLint("CutPasteId")
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
        getPermission()
        PlayResource.getLiveData(this)
        findViewById<RecyclerView>(R.id.rlv_live).layoutManager= LinearLayoutManager(this)
        val adapter= MainLiveAdapter(this)
        adapter.type=0
        findViewById<RecyclerView>(R.id.rlv_live).adapter=adapter
        if(PlayResource.result!=null) {
            adapter.upData(PlayResource.result.data)
        }else{
            Log.d("=====","PlayResource.result=null")
        }

        findViewById<TextView>(R.id.tv_clear_cache).setOnClickListener {
            M3u8FileUtil.clearCache(this,true)
        }

        findViewById<Button>(R.id.btn_list).setOnClickListener {
            val intent=Intent(this,JLVideoDownLoadActivity::class.java)
            startActivity(intent)
        }
    }

    private fun getPermission(){
        PermissionUtil.getPermission(arrayOf(Manifest.permission.WRITE_EXTERNAL_STORAGE),this,true,
            object : PermissionUtil.VerifyPermissionsCallback{
                override fun onPermissionAllGranted() {
                    Log.d("=====","获取到权限")
                }

                override fun onPermissionDeny(permissions: Array<out String>?) {
                    Log.d("=====","已拒绝")
                }

            })
    }

    override fun onRequestPermissionsResult(requestCode: Int, permissions: Array<out String>, grantResults: IntArray) {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults)
        PermissionUtil.onRequestPermissionsResult(requestCode, permissions, grantResults)
    }

    override fun onDestroy() {
        super.onDestroy()
        stopService(Intent(this,M3u8DownLoadService::class.java))
    }
}